/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package muhnes.sizoo.vista;

import com.sun.xml.internal.bind.v2.model.core.ID;
import java.io.Serializable;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import muhnes.sizoo.dal.IDataAccessObject;
import muhnes.sizoo.modelo.Animal;

/**
 *
 * @author CALDERON
 */
public abstract class Convertidor< E, DAO extends IDataAccessObject > 
    implements Converter {
    
    protected abstract DAO getDAO();
    
    protected abstract String idAString( E entidad );
    
    @Override
    public Object getAsObject(FacesContext contexto, UIComponent componente, String valor) {
        Object valorObjeto = null;
        
        if( valor != null && !valor.isEmpty() ) {
            DAO dao = getDAO();
            valorObjeto = dao.buscarPorId( ID.valueOf( valor ) );
        }
        
        return valorObjeto;
    }

    @Override
    public String getAsString(FacesContext contexto, UIComponent componente, Object valor) {
        String valorCadena = "";
        
        if( valor != null && valor instanceof Animal ) {
            valorCadena = idAString( (E)valor );
        }
        
        return valorCadena;
    }

}
